package com.zpark.controller;

import com.zpark.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: ConsumerController
 * @Description:
 * @author: RZS
 * @date: 2024/4/16  10:06
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    // 自动注入restTemplate  实现远程服务调用
    @Autowired
    private RestTemplate restTemplate;

    // 远程服务的URL
    private String url = "http://127.0.0.1:8080/producer/";

    @RequestMapping("/test")
    public void test(){
        System.out.println(restTemplate);
    }

    @GetMapping("/get")
    public User getUser(){
        // 使用restTemplate远程调用Producer服务的接口
        User user = restTemplate.getForObject(url + "get", User.class);
        return user;
    }


    @PostMapping("/post")
    public User postU(){
        // 使用restTemplate远程调用Producer服务的接口
        User user = restTemplate.postForObject(url + "post", null, User.class);
        return user;
    }
}
